//
// = FILENAME
//    readIRs.cc
//
// = AUTHOR(S)
//    Patric Jensfelt
//    
// = COPYRIGHT
//    Copyright (c) 2010 Patric Jensfelt
//
/*----------------------------------------------------------------------*/

#include "Serializer.hh"
#include <iostream>
#include <stdlib.h>
#include <csignal>
#include <cstdio>
#include <string>

bool run = true;
void sigtrap(int) { run = false; }

int main(int argc, char **argv)
{
  const char *optstring = "hd:";
  const char *args = "[-h help] [-d device name]";
  std::string device = "/dev/ttyS2";
  char o = getopt(argc, argv, optstring);
  while (o != -1) {
    switch (o) {
    case 'd':
      device = optarg;
      break;
    case 'h':
    case '?':
      fprintf(stderr, "Usage: %s %s\n", argv[0], args);
      return -1;
    }
    o = getopt(argc, argv, optstring);
  }

  Serializer s;

  s.connect(device);

  s.setUnit(Serializer::UNIT_RAW);

  signal(SIGINT, sigtrap);

  std::cout << "\nPress ctrl-c to stop the program\n\n";
  while (run) {
    std::vector< std::pair<int,int> > query;
    query.resize(5);
    for (unsigned int i = 0; i < query.size(); i++) query[i].first = i;
      
    double t0 = s.getCurrentTime();
    s.readADC(query);
    double dt = s.getCurrentTime()-t0;
    printf("%8.6lf ", dt);
    for (unsigned int i = 0; i < query.size(); i++) {
      std::cout << query[i].second << " ";
    }
    std::cout << std::endl;
  }

  s.disconnect();

  return 0;
}
